package org.tczs93.commonreadwrite.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tczs93.commonreadwrite.mapper.GoodsMapper;
import org.tczs93.commonreadwrite.mapper.OrderMapper;
import org.tczs93.commonreadwrite.mapper.UserMapper;
import org.tczs93.commonreadwrite.model.Goods;
import org.tczs93.commonreadwrite.model.Order;
import org.tczs93.commonreadwrite.model.User;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author ：tao.li
 * @description：
 * @date ：Created in 2021/8/23 16:38
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private OrderMapper orderMapper;

    @Override
    public List<User> getUser() {
        return userMapper.findAll();
    }

    @Override
    public Long add() {
        User user = new User();
        user.setName("李涛");
        user.setAge(23);
        return userMapper.add(user);
    }

    @Override
    public List<User> mix() {
        User user = new User();
        user.setName("李涛");
        user.setAge(23);
        userMapper.add(user);
        return userMapper.findAll();
    }

    @Override
    public List<User> noTran() {
        User user = new User();
        user.setName("李涛");
        user.setAge(23);
        userMapper.add(user);
        Goods goods = new Goods();
        goods.setName("商品1");
        goods.setStock(100);
        goodsMapper.add(goods);
        Order order = new Order();
        order.setGoodsId(goods.getId());
        order.setUserId(user.getId());
        orderMapper.add(order);
        return userMapper.findAll();
    }

    @Transactional
    @Override
    public List<User> tran() {
        User user = new User();
        user.setName("李涛");
        user.setAge(23);
        userMapper.add(user);
        Goods goods = new Goods();
        goods.setName("商品1");
        goods.setStock(100);
        goodsMapper.add(goods);
        Order order = new Order();
        order.setGoodsId(goods.getId());
        order.setUserId(user.getId());
        orderMapper.add(order);
        return userMapper.findAll();
    }

    @Transactional
    @Override
    public Order miaosha() {
        Long goodsId = 1L;
        Goods goods = goodsMapper.findById(goodsId);
        if(goods.getStock() < 1)
            throw new IllegalStateException("没货了");
        Random random = new Random();
        long userId = random.nextInt(10000)+1;
        User user = userMapper.findById(userId);
        if(user == null){
            user = new User();
            user.setName(userId+"");
            user.setAge(30);
            userMapper.add(user);
            userId = user.getId();
        }
        Order order = new Order();
        order.setGoodsId(goodsId);
        order.setUserId(userId);
        orderMapper.add(order);
        Long update = goodsMapper.update(goodsId);
        if(update < 1)
            throw new IllegalStateException("没货了");
        return order;
    }


}
